package com.xnyy.lottery.service;

import com.xnyy.lottery.common.exception.CustomException;
import com.xnyy.lottery.domain.dto.UserPasswordUpdateDTO;
import com.xnyy.lottery.domain.entity.User;
import com.xnyy.lottery.domain.mapper.UserMapper;
//import org.springframework.security.crypto.factory.PasswordEncoderFactories;
//import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @description:
 * @auther: Administrator
 * @date: 2022/9/16 02:16
 */
@Service
public class UserService {
    @Resource
    private UserMapper userMapper;
//    private final PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();

    public User getByUsername(String username) {
        if (username == null) {
            return null;
        }
        return userMapper.selectByUsername(username);
    }
//
//    /**
//     * 修改密码
//     *
//     * @param updateDTO
//     */
//    @Transactional(rollbackFor = Exception.class)
//    public void updatePassword(UserPasswordUpdateDTO updateDTO) {
//        final User user = userMapper.selectById(updateDTO.getId());
//        if (user == null) {
//            throw new CustomException("修改密码失败,密码错误");
//        }
//        if (!passwordEncoder.matches(updateDTO.getOldPassword(), user.getPassword())) {
//            throw new CustomException("修改密码失败,密码错误");
//        }
//        final User updateRecord = new User();
//        updateRecord.setId(updateDTO.getId());
//
//        updateRecord.setPassword(passwordEncoder.encode(updateDTO.getNewPassword()));
//        if (userMapper.updateById(updateRecord) <= 0) {
//            throw new CustomException("修改密码失败");
//        }
//    }
//
//    public static void main(String[] args) {
//        System.out.println(PasswordEncoderFactories.createDelegatingPasswordEncoder().encode("dpyy@245"));
//    }
}
